extends Control

signal joystick_moved(direction: Vector2)
signal joystick_released

var is_dragging = false
var joystick_center: Vector2
var max_distance = 100.0
var deadzone = 0.12

@onready var background = $Background
@onready var knob = $Knob

func _ready():
	# 创建真正的圆形纹理
	_create_circular_textures()
	# 计算摇杆中心
	joystick_center = size / 2
	# 重置摇杆位置
	_reset_joystick()

func _create_circular_textures():
	# 创建背景圆形纹理（灰色，80x80）
	var bg_image = Image.create(80, 80, false, Image.FORMAT_RGBA8)
	bg_image.fill(Color.TRANSPARENT)
	var center = 40
	var radius = 40
	
	for x in range(80):
		for y in range(80):
			var distance = Vector2(x, y).distance_to(Vector2(center, center))
			if distance <= radius:
				bg_image.set_pixel(x, y, Color(0.3, 0.3, 0.3, 0.6))
	
	var bg_texture = ImageTexture.create_from_image(bg_image)
	background.texture = bg_texture
	# 确保使用左上角作为定位基准
	background.centered = false
	background.offset = Vector2.ZERO
	# 将背景图移到Control矩形的中心
	background.position = (size - Vector2(80, 80)) / 2
	
	# 创建摇杆圆形纹理（白色，30x30）
	var knob_image = Image.create(30, 30, false, Image.FORMAT_RGBA8)
	knob_image.fill(Color.TRANSPARENT)
	center = 15
	radius = 15
	
	for x in range(30):
		for y in range(30):
			var distance = Vector2(x, y).distance_to(Vector2(center, center))
			if distance <= radius:
				knob_image.set_pixel(x, y, Color(0.8, 0.8, 0.8, 0.9))
	
	var knob_texture = ImageTexture.create_from_image(knob_image)
	knob.texture = knob_texture
	# 确保使用左上角作为定位基准
	knob.centered = false
	knob.offset = Vector2.ZERO
	# 将摇杆移到Control矩形的中心
	knob.position = (size - Vector2(30, 30)) / 2

func _gui_input(event):
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT:
			if event.pressed:
				_start_drag(event.position)
			else:
				_end_drag()
	elif event is InputEventMouseMotion and is_dragging:
		_update_joystick_position(event.position)

func _start_drag(pos: Vector2):
	# 使用相对于摇杆根节点的坐标
	var local_pos = pos
	var distance = local_pos.distance_to(joystick_center)
	
	# 调试信息
	print("=== 移动摇杆调试 ===")
	print("点击位置: ", pos)
	print("摇杆中心: ", joystick_center)
	print("距离: ", distance)
	print("最大距离: ", max_distance)
	print("==================")
	
	if distance <= max_distance:
		is_dragging = true
		_update_joystick_position(pos)

func _end_drag():
	is_dragging = false
	_reset_joystick()
	joystick_moved.emit(Vector2.ZERO)
	joystick_released.emit()

func _update_joystick_position(pos: Vector2):
	var direction = pos - joystick_center
	var distance = direction.length()
	
	# 限制摇杆移动距离
	if distance > max_distance:
		direction = direction.normalized() * max_distance
		distance = max_distance
	
	# 死区：距离小于阈值则输出零向量
	var normalized_direction = Vector2.ZERO
	if distance < max_distance * deadzone:
		direction = Vector2.ZERO
		distance = 0.0
		normalized_direction = Vector2.ZERO
	else:
		normalized_direction = direction / max_distance

	# 更新摇杆位置（相对于中心位置）
	var center_offset = size / 2 - Vector2(15, 15)
	knob.position = center_offset + direction
	
	# 计算并发送方向信号
	joystick_moved.emit(normalized_direction)

func _reset_joystick():
	knob.position = size / 2 - Vector2(15, 15)
